// pages/my/topUp/topUp.js
var app = getApp(), api = require("../../../api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    priceList:[{
        topUrl:'../../../../image/topup1.png',
        id:'0',
        flag: true
    }, {
        topUrl: '../../../../image/topup2.png',
        id: '1',
        flag: false
      },{
        topUrl: '../../../../image/topup3.png',
        id: '2',
        flag: false
      }],
    currentIndex:'0'
  },
  details: function (e) {

    var title = e.currentTarget.dataset.title, genre = e.currentTarget.dataset.genre;
    wx.navigateTo({
      url: '/pages/details/details?title=' + title + '&genre=' + genre,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  clickPrice: function(e){
    let that = this
    let index = e.currentTarget.dataset.index
    console.log(index)
    // let priceList = this.data.priceList
    // for (var i = 0; i < priceList.length; i++){
    //   priceList[i].flag = false
    // }
    // priceList[index].flag = true
   that.setData({
     currentIndex: index,
    //  priceList
   })
  },
  goAgree:function(){
    wx.navigateTo({
      url: './agree/agree',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this=this;
      app.request({
        url: api.setting,
        method:'post',
        success:function(res){
          _this.setData({
            data: res.data
          })
            
        }
      })
  },
  payment:function(){
    var _this=this;
    if (_this.data.currentIndex==0){
      wx.showToast({
        title: '请选择充值金额',
        icon:'none'
      })
      return false;
    }
    app.request({
      url:api.top_up_payment,
      data:{
        genre: _this.data.currentIndex
      },
      method:'post',
      success:function(res){
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: res.data.signType,
          paySign: res.data.paySign,
          success:function(res){
            wx.showToast({
              title: '支付成功',
              icon:'none'
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})